package ExtendDemo.demo13;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class ThreadServer implements Runnable {
    private Socket s;

    public ThreadServer(Socket s) {
        this.s = s;
    }
    @Override
    public void run() {
        BufferedOutputStream bos = null;
        try {
            InputStream is = s.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(new FileOutputStream(UUID.randomUUID().toString() + ".jpg"));
            int len;
            while ((len = bis.read()) != -1) {
                bos.write(len);
            }
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            bw.write("上传成功");
            bw.newLine();
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (s != null) {
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
